package com.crawler.application;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JProgressBar;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTextPane;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ResultWnd extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MainWndController mController;
	private JTextField textFieldURL1;
	private JProgressBar progressBarURL1;
	private JList<String> listRel;
	private DefaultListModel<String> listModelRel;
	private JTextPane txtpnContentview;
	private JScrollPane scrollPaneContent;
	private JTextField textFieldNewFeature;
	private JButton btnAddFeature;
	private JList<String> listFeature;
	private DefaultListModel<String> listModeFeature;
	private JScrollPane scrollPaneFeature;
	private JButton btnGeneratearff;
	private JLabel lblProcessing_1;
	private JTextField textFieldURL2;
	private JProgressBar progressBarURL2;
	private JList<String> listUnRel;
	private DefaultListModel<String> listModelUnRel;
	private JScrollPane scrollPaneUnRel;
	private JButton btnProcessAllPages;
	private int mCurrentRelSelect;
	private int mResultType;
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					ResultWnd dialog = new ResultWnd(0);
//					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//					dialog.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the dialog.
	 */
	public ResultWnd(int nType, int nMaxPages, MainWndController controller) {
		mResultType = nType;
		mController = controller;
		mCurrentRelSelect = -1;
		setResizable(false);
		setTitle("Crawler Result");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 749, 447);
		getContentPane().setLayout(null);
		
		JLabel lblProcessing = new JLabel("Processing:");
		lblProcessing.setBounds(10, 11, 68, 14);
		getContentPane().add(lblProcessing);
		
		textFieldURL1 = new JTextField();
		textFieldURL1.setEditable(false);
		textFieldURL1.setBounds(88, 8, 236, 20);
		getContentPane().add(textFieldURL1);
		textFieldURL1.setColumns(10);
		
		progressBarURL1 = new JProgressBar();
		progressBarURL1.setBounds(10, 36, 314, 14);
		progressBarURL1.setMaximum(nMaxPages);
		progressBarURL1.setStringPainted(true);
		getContentPane().add(progressBarURL1);
		
		listModelRel = new DefaultListModel<String>();
		listRel = new JList<String>(listModelRel);
		listRel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 1)
				{
					txtpnContentview.setText("");
					mCurrentRelSelect = listRel.getSelectedIndex();
					String strContent = mController.GetPageContent(listRel.getSelectedValue(), true);
					if(mCurrentRelSelect >= 0 && strContent != null)
					{
						txtpnContentview.setText(strContent);
					}
				}
				if(mResultType == 1 && e.getClickCount() ==2)
				{
					mController.SwapGroup(listRel.getSelectedValue(), true);
					txtpnContentview.setText("");
				}
			}
		});

		listRel.setBounds(10, 61, 314, 130);
		//getContentPane().add(listRel);
		JScrollPane scrollPaneRel = new JScrollPane(listRel);
		scrollPaneRel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPaneRel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneRel.setBounds(10, 61, 314, 130);
		getContentPane().add(scrollPaneRel);
		
		txtpnContentview = new JTextPane();
		txtpnContentview.setAutoscrolls(false);
		txtpnContentview.setEditable(false);
		txtpnContentview.setMaximumSize(new Dimension(225, 2147483647));
		txtpnContentview.setBounds(334, 8, 225, 364);

		scrollPaneContent = new JScrollPane(txtpnContentview);
		scrollPaneContent.setMaximumSize(new Dimension(225, 32767));
		scrollPaneContent.setBounds(334, 8, 225, 364);
		//scrollPaneContent.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPaneContent.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		getContentPane().add(scrollPaneContent);
		
		textFieldNewFeature = new JTextField();
		textFieldNewFeature.setBounds(569, 8, 164, 20);
		getContentPane().add(textFieldNewFeature);
		textFieldNewFeature.setColumns(10);
		
		btnAddFeature = new JButton("Add Feature");
		btnAddFeature.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(!textFieldNewFeature.getText().isEmpty())
				{
					mController.AddFeature(textFieldNewFeature.getText());
					textFieldNewFeature.setText("");
				}
			}
		});
		btnAddFeature.setBounds(579, 36, 138, 23);
		getContentPane().add(btnAddFeature);
		
		listModeFeature = new DefaultListModel<String>();
		listFeature = new JList<String>(listModeFeature);
		listFeature.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(mResultType == 0 && e.getClickCount() == 2)
				{
					mController.RemoveFeature(listFeature.getSelectedValue());
				}
			}
		});
		listFeature.setBounds(569, 62, 164, 310);
		scrollPaneFeature = new JScrollPane(listFeature);
		scrollPaneFeature.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneFeature.setBounds(569, 62, 164, 310);
		getContentPane().add(scrollPaneFeature);
		
		btnGeneratearff = new JButton("Generate .arff");
		btnGeneratearff.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				mController.GenerateARFF();
			}
		});
		btnGeneratearff.setBounds(579, 383, 138, 23);
		getContentPane().add(btnGeneratearff);
		
		lblProcessing_1 = new JLabel("Processing:");
		lblProcessing_1.setBounds(10, 234, 68, 14);
		getContentPane().add(lblProcessing_1);
		
		textFieldURL2 = new JTextField();
		textFieldURL2.setEditable(false);
		textFieldURL2.setBounds(88, 231, 236, 20);
		getContentPane().add(textFieldURL2);
		textFieldURL2.setColumns(10);
		
		progressBarURL2 = new JProgressBar();
		progressBarURL2.setBounds(10, 259, 314, 14);
		progressBarURL2.setMaximum(nMaxPages);
		progressBarURL2.setStringPainted(true);
		getContentPane().add(progressBarURL2);
		
		listModelUnRel = new DefaultListModel<String>();
		listUnRel = new JList<String>(listModelUnRel);
		listUnRel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 1)
				{
					txtpnContentview.setText("");
					mCurrentRelSelect = listUnRel.getSelectedIndex();
					String strContent = mController.GetPageContent(listUnRel.getSelectedValue(), false);
					if(mCurrentRelSelect >= 0 && strContent != null)
					{
						txtpnContentview.setText(strContent);
					}
				}
				
				if(mResultType == 1 && e.getClickCount() == 2)
				{
					mController.SwapGroup(listUnRel.getSelectedValue(), false);
					txtpnContentview.setText("");
				}
			}
		});
		listUnRel.setBounds(10, 284, 314, 123);
		scrollPaneUnRel = new JScrollPane(listUnRel);
		scrollPaneUnRel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneUnRel.setBounds(10, 284, 314, 123);
		getContentPane().add(scrollPaneUnRel);
		
		btnProcessAllPages = new JButton("Process All Pages");
		btnProcessAllPages.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				mController.ProcessAllPages();
			}
		});
		btnProcessAllPages.setBounds(363, 383, 170, 23);
		getContentPane().add(btnProcessAllPages);
		
		if(mResultType == 1)
		{
			textFieldNewFeature.setEnabled(false);
			btnAddFeature.setEnabled(false);
		}
	}
	
	public void UpdateProgress(int nProgress, String strURL, boolean bIsRel)
	{
		if(bIsRel)
		{
			progressBarURL1.setValue(nProgress);
			textFieldURL1.setText(strURL);
		}
		else
		{
			progressBarURL2.setValue(nProgress);
			textFieldURL2.setText(strURL);
		}
	}
	
	public void InsertTitle(String strTitle, String strURL, boolean bIsRel)
	{
		if(bIsRel)
			listModelRel.addElement(strURL);
		else
			listModelUnRel.addElement(strURL);
	}
	
	public void RemoveTitle(String strTitle, boolean bIsRel)
	{
		if(bIsRel)
			listModelRel.removeElement(strTitle);
		else
			listModelUnRel.removeElement(strTitle);
	}
	
	public void InsertFeature(String strFeature)
	{
		listModeFeature.addElement(strFeature);
	}
	
	public void RemoveFeature(String strFeature)
	{
		listModeFeature.removeElement(strFeature);
	}
}
